package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.mvp.utils.ToastUtil;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.ResetPassActivityView;
import com.wushi.lenovo.asia5b.app_di.profile.presenter.ResetPassActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.ChangeProfileEvent;
import com.wushi.lenovo.asia5b.app_model.profile.bean.ChangeProfileType;
import com.wushi.lenovo.asia5b.app_utils.WToastHelper;
import com.wushi.lenovo.asia5b.app_utils.eye.EyeUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2018/12/3.
 * 重置登录密码
 */
public class ResetPassActivity extends BaseMvpActivity<
        ResetPassActivityView, ResetPassActivityPresenter> implements ResetPassActivityView {

    @BindView(R.id.et_resetpass_pass)
    EditText et_resetpass_pass;
    @BindView(R.id.et_resetpass_repass)
    EditText et_resetpass_repass;
    @BindView(R.id.bt_resetpass_submit)
    Button bt_resetpass_submit;
    @BindView(R.id.tv_resetpass_invalidpass)
    TextView tv_resetpass_invalidpass;
    @BindView(R.id.iv_resetpass_pass_eye)
    ImageView iv_resetpass_pass_eye;
    @BindView(R.id.iv_resetpass_repass_eye)
    ImageView iv_resetpass_repass_eye;
    private String resetLoginPassEmail;
    private String resetLoginPassCode;
    private String forgot_code;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_resetpass;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        resetLoginPassEmail = getIntent().getStringExtra(CommonConstant.RESET_LOGIN_PASS_EMAIL);
        resetLoginPassCode = getIntent().getStringExtra(CommonConstant.RESET_LOGIN_PASS_CODE);
        forgot_code = getIntent().getStringExtra(CommonConstant.RESET_LOGIN_FORGOT_CODE);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    private void submit() {
        String newPass = et_resetpass_pass.getText().toString().trim();
        String newPass2 = et_resetpass_repass.getText().toString().trim();

        if (TextUtils.isEmpty(newPass)) {
            ToastUtil.showToast("请输入新密码");
            return;
        }
        if (!TextUtils.equals(newPass, newPass2)) {
            ToastUtil.showToast("两次密码不一致");
            return;
        }

        Map<String, Object> map = MapHelper.newHashMap();
        if (!TextUtils.isEmpty(forgot_code)) {
            map.put("forgot_code", forgot_code + "");
            map.put("new_login_password", newPass2 + "");
            getPresenter().resetLoginPass(map, 1);
        } else {
            map.put("email", resetLoginPassEmail + "");
            map.put("new_login_password", newPass);
            map.put("re_type_password", newPass2);
            map.put("otp", resetLoginPassCode + "");
            getPresenter().resetLoginPass(map, 2);
        }

    }

    @Override
    protected void initEvent() {
        EyeUtils.bindEye(iv_resetpass_pass_eye, et_resetpass_pass);
        EyeUtils.bindEye(iv_resetpass_repass_eye, et_resetpass_repass);

    }

    @OnClick({
            R.id.bt_resetpass_submit
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_resetpass_submit:
                submit();
                break;
        }

    }

    @Override
    public void resetLoginPass(ResultModel resultModel) {
        if (resultModel.isSuccess()) {
            WToastHelper.showContentView(this, R.string.setpass_success_notic);
            EventBus.getDefault().post(new ChangeProfileEvent(ChangeProfileType.ChangeLoginPass));
            finish();
        } else {
            tv_resetpass_invalidpass.setVisibility(View.VISIBLE);
            tv_resetpass_invalidpass.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (tv_resetpass_invalidpass != null) {
                        tv_resetpass_invalidpass.setVisibility(View.GONE);
                    }
                }
            }, 3000);
        }
    }
}
